home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 4 / BBS in a Box - Macintosh - Volume IV (January 1992) (BBS in a Box).iso / Files / Word / H / HyperCard Link… < prev    next >
Encoding:
Text File  |  1987-10-22  |  8.2 KB  |  225 lines  |  [TEXT/GEOL]

  1. While developing a HyperCard stack to track new 3rd party products,
  2. I discovered I needed to have a more powerful linking facility than
  3. the one provided by Hypercard. My idea was to create circular links
  4. among all similar products (e.g. all Mac II monitors). However, under
  5. the old system, this was very difficult. Not only was the actual
  6. linking of new objects into an existing circle awkward, but deleting
  7. items was virtually impossible.
  8.  
  9. Imagine a stack linked like the one below. This linking system is
  10. similar to the one used in the Clip Art stack on the Home card:
  11.  
  12.                           +--------+
  13.                    +------| card 4 |<-------+
  14.                    |      +--------+        |
  15.                    v                        |
  16.               +--------+              +--------+
  17.               | card 1 |------------->| card 3 |
  18.               +--------+              +--------+
  19.  
  20.                           +--------+
  21.                           | card 2 |
  22.                           +--------+
  23.  
  24. Card #2 is a newly created card that you want to insert into this
  25. chain somehow. Under the old system, you would have to break the
  26. link between cards 1 and 3, and then create 2 new links between 1->2
  27. and 2->3. This is awkward and time consuming, especially with
  28. having to toggle between the button tool and browse tool all the
  29. time.
  30.  
  31. To speed up the process, I wrote the following button and
  32. background scripts. In addition to making linking a one step process,
  33. it adds a facility for resolving links when a card is deleted.
  34.  
  35. I am submitting these routines for 2 reasons:
  36.  
  37. 1) They are handy, and will hopefully help you in your own
  38. endeavors, and
  39.  
  40. 2) They could do with a bit of help themselves. For example, I have to
  41. keep a list of backward links to facilitate deleting cards. I do this by
  42. adding a comment line at line 2 of my button script that holds the
  43. card ID of the card that points to it. However, my routines don't
  44. handle circular lists that do NOT have this additional comment line.
  45. Therefore, I am posting these routines so that the collective genius of
  46. this group can add to their efficiency and functionality.
  47.  
  48. (Note: in these listings, I have substituted "++" for the funny "option-
  49. return" character).
  50.  
  51. In order to implement these scripts in my stack, I created 2
  52. background buttons. The first one is called "Similar Products". This
  53. button contains the script that moves us from one card to the next
  54. one in the chain. It looks something like this:
  55.  
  56.  
  57. on MouseUp
  58.   -- Linked from card id <####>
  59.   go to card id <####>
  60. end MouseUP
  61.  
  62.  
  63.  This script is created and modified by the second button, called
  64. "Link To". Here is its script:
  65.  
  66.  
  67. on mouseUp  -- "Link to" button was hit
  68.   global LinkFromCard
  69.   Show field "Link To"
  70.   Show background button "Move to card to Link"
  71.   Show background button "to and press:"
  72.   Show background button "OK"
  73.   Show background button "or"
  74.   Show background button "Cancel"
  75.   put the ID of this card into LinkFromCard
  76. end mouseUp
  77.  
  78.  
  79. Clicking on this button makes visible a background field and 2
  80. background buttons -- well, actually 5. I use buttons to contain text
  81. that I want carried from card to card. If I put the text in a
  82. background field, it will not be carried.
  83.  
  84. The "window" looks like this, and appears on top of everything else
  85. (like the normal Link window):
  86.  
  87.             +--------------------------------------+
  88.             |  Move to card to link to and press:  |
  89.             |  +----------+        +------------+  |
  90.             |  |    OK    |   or   |   Cancel   |  |
  91.             |  +----------+        +------------+  |
  92.             +--------------------------------------+
  93.  
  94. In addition to making this window visible, the button also stores the
  95. ID of the new card in the global variable LinkFromCard.
  96.  
  97. The Cancel button of the "link window" is straightforward; it simply
  98. hides all the buttons and the field:
  99.  
  100.  
  101. on mouseUp  -- Cancel button was hit
  102.   hide background button "OK"
  103.   hide background button "or"
  104.   hide background button "Cancel"
  105.   hide background button "Move to card to Link"
  106.   hide background button "to and press:"
  107.   hide field "Link To"
  108. end mouseUp
  109.  
  110.  
  111. The OK button is where the action is. After pressing the "Link to"
  112. button to show the "link window", we can move anywhere within the
  113. circular link (using any means, such as arrows or command-F).
  114. Pressing the OK button will form the new links, using the following
  115. script:
  116.  
  117.  
  118. on mouseUp  -- OK button was hit
  119.   Global LinkFromCard
  120.   -- check to see that we aren't linking to ourselves
  121.   if LinkFromCard is the ID of this card then
  122.     answer "Sorry, I won't link this card to itself." with "Cancel"
  123.   else
  124.     put word 3 of LinkFromCard into LinkID -- Actual card number
  125.     Get script of button "Similar Products"
  126.     if it is not empty then  -- insert into cycle
  127.       -- move the link script from this card (1) to our new card (2)
  128.       -- and change the backlink
  129.       Get script of button "Similar Products"
  130.       put the ID of this card into word 4 to 6 of line 2 of it
  131.       Set script of button "Similar Products" of card id LinkID to it
  132.       Put word 5 of line 3 of it into TempID  -- for later
  133.       --
  134.       -- Create forelink from this card (1) to new card (2)
  135.       Get script of button "Similar Products"
  136.       put LinkID into word 5 of line 3 of it
  137.       Set script of button "Similar Products" to it
  138.       --
  139.       -- Modify backlink of card (3) to new card (2)
  140.       Get the script of button "Similar Products" of card id TempID
  141.       put LinkID into word 6 of line 2 of it
  142.       Set the script of button "Similar Products" of ++
  143.       card id TempID to it
  144.     else  -- link the two together (no previous link in this card)
  145.       -----------
  146.       Set script of button "Similar Products" to ++
  147.       "on mouseUp" & RETURN & ++
  148.       "-- Linked from " & LinkFromCard & RETURN & ++
  149.       "go to " & LinkFromCard & RETURN & ++
  150.       "end mouseUp" & RETURN
  151.       -----------
  152.       Set script of button "Similar Products" of card id LinkID to ++
  153.       "on mouseUp" & RETURN & ++
  154.       "-- Linked from " & the id of this card & RETURN & ++
  155.       "go to " & ID of this card & RETURN & ++
  156.       "end mouseUp" & RETURN
  157.       -----------
  158.     end if
  159.   end if
  160.   hide background button "OK"
  161.   hide background button "or"
  162.   hide background button "Cancel"
  163.   hide background button "Move to card to Link"
  164.   hide background button "to and press:"
  165.   hide field "Link To"
  166. end mouseUp
  167.  
  168.  
  169. Got it? Fine. For those those that missed it, here's what happened:
  170.  
  171. 1) First a check is made to see if we are trying to the link the new
  172. card (referred to in the script as card #2) to itself.
  173.  
  174. 2) If so, then we say "Sorry, I won't link this card to itself", hide the
  175. buttons, and exit.
  176.  
  177. 3) If not, we check if we are linking into an existing circular link.
  178.  
  179. 4) If so, we break the chain, modify card #1 to point to card #2, point
  180. the backlink of card #3 to card #2, and insert a script into card #2
  181. that points forward to card #3 and backwards to card #1.
  182.  
  183. 5) If we are not inserting the new card into a current link, then we
  184. are essentially creating a new circular link, and the appropriate
  185. scripts are inserted.
  186.  
  187. 6) After all is said and done, the buttons are hidden.
  188.  
  189. All that remains is to resolve links when a card is deleted. The
  190. following background script accomplishes that:
  191.  
  192.  
  193. on DeleteCard  -- in background script
  194.   -- Resolve links in "Similar Products" button
  195.   get the script of button "Similar Products"
  196.   put word 6 of line 2 of it into BackLink
  197.   put word 5 of line 3 of it into ForeLink
  198.   if BackLink is not empty then
  199.     if BackLink <> ForeLink then
  200.       Get script of button "Similar Products" of card id ForeLink
  201.       Put BackLink into word 6 of line 2 of it
  202.       Set script of button "Similar Products" of card id ForeLink to it
  203.       Get script of button "Similar Products" of card id BackLink
  204.       Put ForeLink into word 5 of line 3 of it
  205.       Set script of button "Similar Products" of card id BackLink to it
  206.     else
  207.       Set script of button "Similar Products" of card id BackLink ++
  208.       to empty
  209.     end if
  210.   end if
  211. end DeleteCard
  212.  
  213.  
  214. This script first checks to see if this card's linking button has a script.
  215. If so, it identifies the cards on either side of the "to-be-nuked" card,
  216. patches their scripts, and exits to HyperCard, which will then delete
  217. the card.
  218.  
  219. -----
  220.  
  221. Keith Rollin
  222. Technical Communications
  223. Sales Tech Support
  224.  
  225.